package com.ibuu.demos.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ibuu.demos.pojo.Categories;
import com.ibuu.demos.pojo.User;
import com.ibuu.demos.service.AdminService;
import com.ibuu.demos.service.CategoriesService;
import com.ibuu.dto.LoginRequestDTO;
import com.ibuu.dto.OrderDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class adminController {

    @Autowired
    private AdminService service;

    @Autowired
    private CategoriesService categoriesService;

    @PostMapping("/login")
    public ResponseEntity<String> login(@RequestBody LoginRequestDTO loginRequest, HttpServletRequest request) {
        boolean login = service.login(loginRequest.getUsername(), loginRequest.getPassword());
        if (login) {
            request.getSession().setAttribute("username", loginRequest.getUsername());
            return ResponseEntity.ok("Login successful");
        } else {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid username or password");
        }
    }

    @GetMapping("/select")
    public List<User> select() {
        Page<User> select = service.select();
        return select.getRecords();

    }

    @DeleteMapping("/delete/{id}")
    public String deleteUser(@PathVariable("id") Integer id) {
        service.removeById(id);
        return "success";
    }

    @PostMapping("/upload")
    public ResponseEntity<String> addProduct(@RequestBody Categories categories) {

        categoriesService.add(categories);
        return ResponseEntity.ok("添加成功");
    }

    @PostMapping("/genggai")
    public ResponseEntity<String> updateProduct(@RequestBody Categories categories) {
        categoriesService.updateid(categories);
        return ResponseEntity.ok("修改成功");
    }

    @DeleteMapping("/{id}")
    public String deleteProduct(@PathVariable("id") Integer id) {
        categoriesService.removeid(id);
        return "success";
    }

    @GetMapping("/get")
    public Page<OrderDTO> get(int pageNum, int pageSize) {
        Page<OrderDTO> details = categoriesService.admingetDetails(pageNum, pageSize);
        return details;
    }


//    @PostMapping("/upload")
//    public ResponseEntity<Map<String, String>> uploadFile(@RequestParam("file") MultipartFile file) {
//        if (file.isEmpty()) {
//            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(Collections.singletonMap("error", "File is empty"));
//        }
//
//        try {
//            // Save the file to a directory
//            String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
//            Path path = Paths.get("uploads/" + fileName);
//            Files.copy(file.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);
//
//            // Return the URL of the uploaded file
//            String fileUrl = "/uploads/" + fileName;
//            return ResponseEntity.ok(Collections.singletonMap("url", fileUrl));
//        } catch (IOException e) {
//            e.printStackTrace();
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Collections.singletonMap("error", "File upload failed"));
//        }
//    }
//    @PostMapping("/upload")
//    public ResponseEntity<String> addProduct(
//            @RequestParam("name") String name,
//            @RequestParam("price") Integer price,
//            @RequestParam("kind") String kind,
//            @RequestParam("image") MultipartFile image) {
//
//        // 这里可以保存文件到服务器
//        String imageUrl = saveFile(image); // 假设你有一个方法保存文件并返回文件路径
//
//        // 创建Categories对象并设置数据
//        Categories categories = new Categories();
//        categories.setName(name);
//        categories.setPrice(price);
//        categories.setKind(kind);
//        categories.setImage(imageUrl); // 保存图片路径
//
//        // 调用服务层保存商品
//        categoriesService.add(categories);
//
//        return ResponseEntity.ok("添加成功");
//    }
//
//    // 保存文件的方法
//    private String saveFile(MultipartFile file) {
//        // 处理文件保存逻辑，例如保存到某个目录
//        // 并返回文件的URL或路径
//        String fileName = file.getOriginalFilename();
//        Path path = Paths.get("uploads", fileName);  // 假设文件保存在uploads目录下
//        try {
//            Files.copy(file.getInputStream(), path);
//        } catch (IOException e) {
//            e.printStackTrace();
//            return null;
//        }
//        return "/uploads/" + fileName;  // 返回文件的URL路径
//    }

}
